package com.yueya.common.util;

import java.util.List;

import com.alicloud.openservices.tablestore.model.Column;
import com.alicloud.openservices.tablestore.model.ColumnValue;
import com.alicloud.openservices.tablestore.model.GetRowRequest;
import com.alicloud.openservices.tablestore.model.GetRowResponse;
import com.alicloud.openservices.tablestore.model.PrimaryKey;
import com.alicloud.openservices.tablestore.model.PrimaryKeyBuilder;
import com.alicloud.openservices.tablestore.model.PrimaryKeyValue;
import com.alicloud.openservices.tablestore.model.Row;
import com.alicloud.openservices.tablestore.model.SingleRowQueryCriteria;
import com.jfinal.kit.PropKit;
import com.yueya.config.TableKit;

/**
 * 短信验证 工具类型 
 * @author wuenyang
 *
 */
public class SmsUtil {
	
	
	public static boolean verifySms(String tel ,String veriCode){
	 
		PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
		primaryKeyBuilder.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString(tel));
		PrimaryKey primaryKey = primaryKeyBuilder.build();
        boolean flag = false;
		// 读一行
		SingleRowQueryCriteria criteria = new SingleRowQueryCriteria(TableKit.SMS_CODE, primaryKey);
		// 设置读取最新版本
		criteria.setMaxVersions(1);
		GetRowResponse getRowResponse = TableKit.getClient().getRow(new GetRowRequest(criteria));
		Row row = getRowResponse.getRow();
		if (row != null) {
			List<Column> list = row.getColumn("code");

			String code = "";
			if (list != null) {
				Column m = (Column) list.get(0);
				ColumnValue cv = m.getValue();
				code = cv.asString();
			}

			String createtime = "";
			list = row.getColumn("createtime");
			if (list != null) {
				Column m = (Column) list.get(0);
				ColumnValue cv = m.getValue();
				createtime = cv.asString();
			}

			String tel1 = "";
			list = row.getColumn("tel");
			if (list != null && list.size() > 0) {
				Column m = (Column) list.get(0);
				ColumnValue cv = m.getValue();
				tel1 = cv.asString();
			}

			if (!"".equals(code) && !"".equals(createtime) && tel1.equals(tel)) {//
				String rcode = code;//
				String ctime = createtime;//
				if (veriCode == rcode || veriCode.equals(rcode)) {
					long currentTime = System.currentTimeMillis();
					long m = currentTime - Long.valueOf(ctime);
					long activeTime = 1000 * 60 * PropKit.use("config.properties").getLong("sms.activetime");// 手机验证码有10分钟的有效期
					// boolean timeBl = false;
					 if (m <= activeTime ? true : false) {
						flag = true;
			         }
		
				}
			}
		}
		return flag;
    }
	
	public static void main(String[] args) {
		String time = PropKit.use("config.properties").get("sms.activetime");
		System.out.println(time);
	}
}
	
